<?php
/**
 * @version		$Id: factory.php 19176 2010-10-21 03:06:39Z ian $
 * @copyright	Copyright (C) 2005 - 2010 Open Source Matters. All rights reserved.
 * @license		GNU/GPL, see LICENSE.php
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 * See COPYRIGHT.php for copyright notices and details.
 */
defined('PATH_BASE') or die();
/**
 *  Framework Factory class
 *
 * @static
 * @since	1.5
 */
require_once (PATH_SITE.DS.'libraries'.DS.'loader.php');

class Factory
{
	/**
	 * Get a application object
	 *
	 * Returns a reference to the global {@link JApplication} object, only creating it
	 * if it doesn't already exist.
	 *
	 * @access public
	 * @param	mixed	$id 		A client identifier or name.
	 * @param	array	$config 	An optional associative array of configuration settings.
	 * @return object JApplication
	 */
	function &getApplication($id = null, $config = array(), $prefix='J')
	{
		
	}

	/**
	 * Get a configuration object
	 *
	 * Returns a reference to the global {@link JRegistry} object, only creating it
	 * if it doesn't already exist.
	 *
	 * @access public
	 * @param string	The path to the configuration file
	 * @param string	The type of the configuration file
	 * @return object JRegistry
	 */
	function &getConfig($file = null, $type = 'PHP')
	{
		static $instance;
		
		if (!is_object($instance))
		{
			if ($file === null) {
				$file = dirname(__FILE__).DS.'config.php';
				require_once $file;
			}
			$CONFIG = new Config();
			$instance = new APPFrameworkConfig();
			$instance->loadObject($CONFIG);
		}
		return $instance;
	}

	/**
	 * Get a database object
	 *
	 * Returns a reference to the global {@link JDatabase} object, only creating it
	 * if it doesn't already exist.
	 *
	 * @return object JDatabase
	 */
	function &getDBO()
	{
		static $instance;

		if (!is_object($instance))
		{
			//get the debug configuration setting
			$conf =& Factory::getConfig();
			
			$instance = Factory::_createDBO();
		}

		return $instance;
	}

	/**
	* Get an editor object
	*
	* @access public
	* @param string $editor The editor to load, depends on the editor plugins that are installed
	* @return object JEditor
	*/
	function &getEditor($editor = null)
	{
		APPimport( 'app.html.editor' );

		//get the editor configuration setting
		if(is_null($editor))
		{
			$conf =& Factory::getConfig();
			$editor = $conf->getValue('editor');
		}

		$instance =& Editor::getInstance($editor);

		return $instance;
	}


	/**
	 * Create an database object
	 *
	 * @access private
	 * @return object JDatabase
	 * @since 1.5
	 */
	function &_createDBO()
	{	
		APPimport('app.database.database');
		APPimport( 'app.database.table' );

		$conf =& Factory::getConfig();

		$host 		= $conf->getValue('host');
		$user 		= $conf->getValue('user');
		$password 	= $conf->getValue('password');
		$database	= $conf->getValue('db');
		$prefix 	= $conf->getValue('dbprefix');
		$driver 	= $conf->getValue('dbtype');
		$debug 		= $conf->getValue('debug');

		$options	= array ( 'driver' => $driver, 'host' => $host, 'user' => $user, 'password' => $password, 'database' => $database, 'prefix' => $prefix );

		$db =& APPDatabase::getInstance( $options );
		return $db;
	}
}
